import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref<any>()
    const UserInfo = ref<any>()
    // 保存会员信息，登录时使用
    const setProfile = (val: any) => {
      profile.value = val
    }

    // 清理会员信息，退出时使用
    const clearProfile = () => {
      profile.value = undefined
    }
    // 获取会员信息
    const setUserInfo = (val: any) => {
      UserInfo.value = val
    }
    // 清理会员信息，退出时使用
    const clearUserInfo = () => {
      UserInfo.value = undefined
    }
    // 记得 return
    return {
      UserInfo,
      setUserInfo,
      clearUserInfo,
      profile,
      setProfile,
      clearProfile,
    }
  },
  // TODO: 持久化
  {
    persist: {
      storage: {
        getItem: (key: string) => {
          return uni.getStorageSync(key)
        },
        setItem: (key: string, value: any) => {
          // console.log(key, value)
          uni.setStorageSync(key, value)
        },
      },
      paths: ['profile', 'UserInfo'],
    },
  },
)
